-- UIUsePuzzles
-- Created by huanghr
-- 使用罐子界面
-- （该界面的使用罐子功能废除）

UIUsePuzzles = class("UIUsePuzzles", function()
    return cc.Layer:create();
end);

function UIUsePuzzles.create(goodsId, priceType)
    return UIUsePuzzles.new(goodsId, priceType);
end

-- 内部函数声明
local resize;
local relicIds = {6000, 6010, 6020};
-- 构造函数
function UIUsePuzzles:ctor(goodsId, priceType)
    self:setName("UIUsePuzzles");
    self.goodsId = goodsId;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/shop/UsePuzzle.csb");
    self:addChild(node);
    self.node = node;

    self.priceType = priceType;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册各种点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 注册点击事件
function UIUsePuzzles:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIUsePuzzles");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 使用个数按钮文本
    local str = getLocStr("btn_text_use_amount");
    -- 注册使用按钮点击事件
    local btnUse = findChildByName(self.node, "CT/btn_use_one");
    local function onUseClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 尝试使用
            self:tryUse(1);
        end
    end
    btnUse:addTouchEventListener(onUseClick);
    TextStyleM.setTextStyle(btnUse, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnUse:setTitleText(string.format(str, 1));

    -- 注册使用按钮点击事件
    local btnUse = findChildByName(self.node, "CT/btn_use_multi");
    local function onUseClick2(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 尝试使用
            self:tryUse(5);
        end
    end
    btnUse:addTouchEventListener(onUseClick2);
    TextStyleM.setTextStyle(btnUse, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnUse:setTitleText(string.format(str, 5));
end

-- 注册事件处理回调函数
function UIUsePuzzles:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIUsePuzzles 界面析构清理");
            EventMgr.removeAll("UIUsePuzzles");
        elseif ev == "enter" then

        end
    end);

    -- 使用罐子事件回调
    EventMgr.register("UIUsePuzzles", event.USE_PUZZLE_BOX, function(args)
        -- 直接关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIUsePuzzles");
        return;
    end);

     -- 使用遗物事件
    EventMgr.register("UIUsePuzzles", event.USE_HERO_RELIC, function(args)
        -- 直接关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIUsePuzzles");
        return;
    end);
end

-- 重绘
function UIUsePuzzles:redraw()
    -- 显示商品名称
    local CT = findChildByName(self.node, "CT");
    local nameLabel = CT:getChildByName("name");

    -- 获取商品信息
    local itemName = ItemM.query(self.goodsId, "name");
    local desc = ItemM.query(self.goodsId, "desc");
    local detail = ItemM.query(self.goodsId, "detail");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(itemName);

    -- 商品描述
    local descLabel = findChildByName(CT, "desc");
    local detailLabel = findChildByName(CT, "detail");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    descLabel:setString(desc);
    detailLabel:setString(detail);

    -- 按钮置灰判断
    local btnOne = findChildByName(CT, "btn_use_one");
    local btnMulti = findChildByName(CT, "btn_use_multi");

    if ItemM.getAmount(ME.user, self.goodsId) >= 1 then
        btnOne:setBright(true);
        btnOne:setTouchEnabled(true);
    else
        btnOne:setBright(false);
        btnOne:setTouchEnabled(false);
    end

    if ItemM.getAmount(ME.user, self.goodsId) >= 5 then
        btnMulti:setBright(true);
        btnMulti:setTouchEnabled(true);
    else
        btnMulti:setBright(false);
        btnMulti:setTouchEnabled(false);
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local fixBottom = nil;
    local fixTop = nil;
--    if self.priceType ~= "gem" then
--        -- 自适应压缩/放大可能背景有差异，这个手动修改一下
        fixBottom = AlignM.getAdaptDefaultMargin();
--        fixTop = AlignM.getAdaptDefaultMargin() - 10;
--    end
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { nameLabel, descLabel, detailLabel, btnOne}, {nil, nil, 17}, fixTop, fixBottom);
    btnMulti:setPositionY(btnOne:getPositionY());
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试购买
function UIUsePuzzles:tryUse(amount)
    if ItemM.getAmount(ME.user, self.goodsId) < amount then
        showHint(string.format(getLocStr("no_enough_for_use"), ItemM.query(self.goodsId, "name")));
        return;
    end

    -- 使用
    if table.indexOf(relicIds, self.goodsId) == -1 then
        -- Operation.cmd_use_puzzle_box(self.priceType, amount);
    else
        Operation.cmd_use_hero_relic(self.goodsId, amount);
    end
end